$(document).ready(function(){
	var imageMain = $('#gallery-image-main');
	var imagesList = $('#gallery-image-list');

	function imageMainUrl(url) {
		var image = url.match(/\/image\/([\[\]+\wd\.\-\_\%]+)/i);
		if (image[1] != 'undefined') {
			imageMain.attr('src','upload/gallery/thumb2/' + image[1]);
		}
	}

	$('.gallery-image').click(function(){
		imageMainUrl(this.href);
		
		imagesList.find('.selected').removeClass('selected');
		$(this).parents('li').addClass('selected');
		return false;
	});

	$('#gallery-image-main').click(GImageNext);
	$('#gallery-image-navigation .next').click(GImageNext);
	$('#gallery-image-navigation .prev').click(GImagePrev);
	
	$.hotkeys.add('j', GImagePrev);
	$.hotkeys.add('left', GImagePrev);
	$.hotkeys.add('k', GImageNext);
	$.hotkeys.add('right', GImageNext);
	$.hotkeys.add('space', GImageNext);
	
	// zdiecie nastepne
	function GImageNext (){
		var current = imagesList.find('.selected')
		var next 	= current.next();

		if (next.length < 1) return false;

		current.removeClass('selected');
		next.addClass('selected');

		imageMainUrl(next.find('a').attr('href'));

		return false;
	}

	// zdiecie poprzednie
	function GImagePrev (){
		var current = imagesList.find('.selected')
		var prev 	= current.prev();

		if (prev.length < 1) return false;

		current.removeClass('selected');
		prev.addClass('selected');

		imageMainUrl(prev.find('a').attr('href'));

		return false;
	}
});